package com.miniprogram.common.enums;

import com.google.common.collect.ImmutableMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义异常enum
 *
 * @author lin
 * @date 2018年11月22日
 */
public enum RestStatusEnum {

    /**
     * 枚举信息
     */
    SUCCESS(200, "操作成功！"),
    LOGIN_SUCCESS(201, "登录成功！"),
    ERROR(1000, "服务器未知错误！"),
    UNAUTHORIZED(1001, "尚未登录！"),
    FORBIDDEN(1002, "您没有操作权限！"),
    LOGIN_ERROR(1003, "登陆失败！"),
    PARAMETER_NOTNULL(1004, "参数不能为空！"),
    USER_NOTFOUND(1005, "用户不存在"),
    REGISTER_WRONG(1006, "注册失败"),
    PHONE_NOTNULL(1007, "手机号不能为空"),
    PASSWORD_NOTNULL(1008, "密码不能为空"),
    ID_NOTNULL(1009, "id不能为空"),
    CODE_NOTNULL(1010, "验证码不能为空"),
    PASSWORD_NOTEQUALS(1011, "两次密码不一致"),
    PAY_ERROR(1012, "支付失败"),
    SMSCODE_NOT_FOUND(1013, "未查询到验证码"),
    SMSCODE_EXPIRE(1014, "验证码已过期，请重新发送"),
    SMSCODE_WRONG(1015, "验证码错误，请重新尝试"),
    PASSWORD_WRONG(1016, "密码错误"),
    COUPON_USED(1017, "优惠券已经使用过"),
    COUPON_EXPIRE(1018, "优惠券已过期"),
    FAILED_UPLOAD(1019, "上传失败!"),
    NOT_NULL_FOLDER_ENUM(1020, "上传种类不能为空!"),
    NOT_NULL_UPLOAD_FILE(1021, "上传文件不能为空!"),
    OTHER(1550, "其他");

    private Integer code;

    private String value;

    private static final ImmutableMap<Integer, RestStatusEnum> CACHE;

    static {
        final ImmutableMap.Builder<Integer, RestStatusEnum> builder = ImmutableMap.builder();
        for (RestStatusEnum restStatusEnum : values()) {
            builder.put(restStatusEnum.code(), restStatusEnum);
        }
        CACHE = builder.build();
    }

    RestStatusEnum(Integer code, String value) {
        this.code = code;
        this.value = value;
    }

    public Integer code() {
        return code;
    }

    public String value() {
        return value;
    }

    public static RestStatusEnum valueOfCode(int code) {
        final RestStatusEnum status = CACHE.get(code);
        if (status == null) {
            throw new IllegalArgumentException("No matching constant for [" + code + "]");
        }
        return status;
    }

    public static RestStatusEnum getByValue(String message) {
        for (RestStatusEnum restStatusEnum : RestStatusEnum.values()) {
            if (restStatusEnum.value.equals(message)) {
                return restStatusEnum;
            }
        }
        return RestStatusEnum.ERROR;
    }

    public static List<RestStatusEnum> getAll() {
        List<RestStatusEnum> list = new ArrayList<>();
        for (RestStatusEnum restStatusEnum : RestStatusEnum.values()) {
            list.add(restStatusEnum);
        }
        return list;
    }

    public static Map<Integer, String> getAllForMap() {
        Map<Integer, String> map = new HashMap<>();
        for (RestStatusEnum restStatusEnum : RestStatusEnum.values()) {
            map.put(restStatusEnum.code, restStatusEnum.value);
        }
        return map;
    }

    @Override
    public String toString() {
        return String.valueOf(code);
    }
}
